home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-02-27 | 12.2 KB | 365 lines | [TEXT/CWIE] |
- // This file contains useful routines for converting
- // various data type into human-readable strings.
- //
- // 9/16/94 david first cut
- // 9/20/95 david improved comments
-
- #include <TextEdit.h>
- #include <Errors.h>
- #include <TextUtils.h>
- #include <Resources.h>
- #include <Script.h>
-
- #include "stringUtils.h"
-
-
- /**\
- |**| ==============================================================================
- |**| PRIVATE TYPEDEFS
- |**| ==============================================================================
- \**/
- typedef struct xtnd80
- {
- short exp;
- short man[4];
- } xtnd80 ;
-
-
- /**\
- |**| ==============================================================================
- |**| PUBLIC FUNCTIONS
- |**| ==============================================================================
- \**/
-
-
- /*------------------------------------------------------------------------------*\
- StringToString
- *------------------------------------------------------------------------------*
- This routine copies one pstring into another.
- It assumes that the dest string is long enough to hold the source string.
- \*------------------------------------------------------------------------------*/
- void StringToString ( StringPtr src, StringPtr dest )
- {
- BlockMove(src, dest, src[0]+1) ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- OSTypeToString
- *------------------------------------------------------------------------------*
- This routine converts a 4-byte OSType into a pascal string.
- It assumes that the dest string (which will be 6 chars)
- is long enough to hold the result.
- \*------------------------------------------------------------------------------*/
- void OSTypeToString ( OSType type, StringPtr dest )
- {
- dest[0] = 6 ;
- dest[1] = dest[6] = '\'' ; // put single quotes aroung the ostype
- BlockMove(&type, &dest[2], 4) ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- FSSpecToString
- *------------------------------------------------------------------------------*
- This routine converts the full path of a FSSpec into a pascal string.
- If the appendName parameter is true then the FSSpec's filenam is
- appended to the path.
- It assumes that the dest string (which can be up to 255 chars)
- is long enough to hold the result.
- \*------------------------------------------------------------------------------*/
- void FSSpecToString ( FSSpec spec, StringPtr dest, Boolean appendName )
- {
- CInfoPBRec pb;
- Str255 dirName;
- OSErr err ;
-
- dest[0] = 0; // initialize full pathname
-
- if (appendName)
- pStrCat(dest, spec.name, 255) ;
-
- pb.dirInfo.ioNamePtr = dirName;
- pb.dirInfo.ioVRefNum = spec.vRefNum; // indicat target volume
- pb.dirInfo.ioDrParID = spec.parID; // initialize parent directory ID
- pb.dirInfo.ioFDirIndex = -1; // get info about a directory
-
- do
- {
- pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
- err = PBGetCatInfoSync(&pb) ; // needs error cheching...
- pStrCat(dirName, "\p:", 255) ;
- pStrIns(dest, dirName, 255) ; // assume fullPathName is a Str255
- } while (pb.dirInfo.ioDrDirID != fsRtDirID) ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- LongHexToString
- *------------------------------------------------------------------------------*
- This routine converts a long into a pascal string in hex format.
- It assumes that the dest string (which will be 10 chars)
- is long enough to hold the result.
- \*------------------------------------------------------------------------------*/
- void LongHexToString ( long num, StringPtr dest )
- {
- long n ;
- short i ;
-
- dest[0] = 10 ;
- dest[1] = '0' ;
- dest[2] = 'x' ;
-
- for (i=0; i<8; i++)
- {
- n = ( num & 15 ) ;
- dest[10-i] = (n<=9) ? ('0'+n) : ('A'+n-10) ;
- num >>= 4 ;
- }
- }
-
-
- /*------------------------------------------------------------------------------*\
- VersionToString
- *------------------------------------------------------------------------------*
- This routine converts a long into a pascal string according to the
- convention for encoding version numbers.
- It assumes that the dest string (which may be from 4 to 9 chars)
- is long enough to hold the result.
- \*------------------------------------------------------------------------------*/
- void VersionToString ( long vers, StringPtr dest )
- {
- register unsigned char len = 0 ;
- register unsigned char digit ;
-
- digit = (vers & 0xF0000000) >> 28 ;
- if (digit)
- dest[ ++len ] = '0' + digit ;
-
- digit = (vers & 0x0F000000) >> 24 ;
- dest[ ++len ] = '0' + digit ;
-
- dest[ ++len ] = '.' ;
-
- digit = (vers & 0x00F00000) >> 20 ;
- dest[ ++len ] = '0' + digit ;
-
-
- digit = (vers & 0x000F0000) >> 16 ;
- if (digit)
- {
- dest[ ++len ] = '.' ;
- dest[ ++len ] = '0' + digit ;
- }
-
- digit = (vers & 0x0000FF00) >> 8 ;
- switch (digit)
- {
- case 0x20: dest[ ++len ] = 'd'; break;
- case 0x40: dest[ ++len ] = 'a'; break;
- case 0x60: dest[ ++len ] = 'b'; break;
- }
-
- digit = (vers & 0x000000F0) >> 4 ;
- if (digit)
- dest[ ++len ] = '0' + digit ;
-
- digit = (vers & 0x0000000F) >> 0 ;
- if (digit)
- dest[ ++len ] = '0' + digit ;
-
- dest[0] = len;
- }
-
-
- /*------------------------------------------------------------------------------*\
- pStrCat
- *------------------------------------------------------------------------------*
- This routine concatonates the src pstring to the end of the dest pstring.
- It will not make the dst string longer than the size parameter.
- \*------------------------------------------------------------------------------*/
- void pStrCat ( StringPtr dst, StringPtr src, unsigned char size )
- {
- if (*dst + *src > size) // make sure were not too big
- *src = size - *dst;
- BlockMove(src + 1, dst + *dst + 1, *src) ; // copy string in
- *dst += *src; // adjust length byte
- }
-
-
- /*------------------------------------------------------------------------------*\
- pStrIns
- *------------------------------------------------------------------------------*
- This routine inserts the src pstring at the beginning of the dest pstring.
- It will not make the dst string longer than the size parameter.
- \*------------------------------------------------------------------------------*/
- void pStrIns ( StringPtr dst, StringPtr src, unsigned char size )
- {
- if (*dst + *src > size) // make sure were not too big
- *dst = size - *src;
- BlockMove(dst + 1, dst + *src + 1, *dst) ; // make room for new string
- BlockMove(src + 1, dst + 1, *src) ; // copy new string in
- *dst += *src; // adjust length byte
- }
-
-
- /*------------------------------------------------------------------------------*\
- MyReplaceText
- *------------------------------------------------------------------------------*
- This routine provides a slightly different way to call ReplaceText()
- The only difference is that the parameter that contains the replacement
- text is a StringPtr instead of a handle.
- If the result of this routine is negative, then it is an error code.
- If positive, then it indicates the number of substitutions.
- \*------------------------------------------------------------------------------*/
- short MyReplaceText ( Handle dest, StringPtr rplc, Str15 key )
- {
- Size rplcLen ;
- Handle rplcHandle ;
- short returnVal ;
- OSErr err ;
-
- rplcLen = rplc[0];
- err = PtrToHand( &rplc[1], &rplcHandle, rplcLen ) ;
- if (err) return err ;
-
- returnVal = ReplaceText( dest, rplcHandle, key ) ;
- DisposeHandle( rplcHandle ) ;
- return returnVal ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- MyTETextBox
- *------------------------------------------------------------------------------*
- This routine provides an alternative to calling TETextBox()
- The difference is that this routine expicitly allocate a TEHandle so that
- it can use the text mode of the current GrafPort.
- \*------------------------------------------------------------------------------*/
- void MyTETextBox ( Ptr text, long length, Rect *box, short justType)
- {
- TEHandle hTE ;
-
- hTE = TENew( box, box ) ;
-
- TESetAlignment( justType, hTE ) ;
- TESetText( text, length, hTE ) ;
-
- (**hTE).txMode = (qd.thePort)->txMode ;
-
- TEUpdate( box, hTE ) ;
- TEDispose(hTE) ;
- }
-
-
-
- /*------------------------------------------------------------------------------*\
- FormatExtd80
- *------------------------------------------------------------------------------*
- This routine converts an extended80 number into a pascal string using
- a 'FMAT' resource and the ExtendedToString() call so that the resulting
- string will be formatted accrding to the user's setting in the Numbers
- control panel.
- It assumes that the dest string (which can be up to 255 chars)
- is long enough to hold the result.
- \*------------------------------------------------------------------------------*/
- OSErr FormatExtd80 ( extended80 *x, short formatID, StringPtr dest )
- {
- NumberParts parts;
- NumFormatString **format;
- Intl0Hndl itl4;
- long offset, length;
- FormatStatus status;
-
- /* Just in case! */
- dest[0] = 0;
-
- /* Get default Number Parts resource/offset/length */
- GetIntlResourceTable( iuCurrentScript, iuNumberPartsTable,
- (Handle*)&itl4, &offset, &length ) ;
- if ( itl4==nil ) return paramErr ;
-
- /* Retrieve default Number Parts Table */
- parts = *( NumberPartsPtr )( *((Handle)itl4) + offset ) ;
-
- /* Retrieve canonical format */
- format = ( NumFormatString ** )GetResource( 'FMAT', formatID ) ;
- if ( format==nil ) return resNotFound;
-
- /* Lock it */
- HLock(( Handle )format ) ;
-
- /* Format it! */
- status = ExtendedToString( x, *format, &parts, dest ) ;
-
- /* Unlock and release the canonical format */
- HUnlock( (Handle)format ) ;
- ReleaseResource( (Handle)format ) ;
-
- return status;
- }
-
-
- /*------------------------------------------------------------------------------*\
- FormatLong
- *------------------------------------------------------------------------------*
- This routine converts a long integer into a pascal string using
- a 'FMAT' resource and the ExtendedToString() call so that the resulting
- string will be formatted accrding to the user's setting in the Numbers
- control panel. It does this by converting the long into an extended80
- and then calling FormatExtd80.
- It assumes that the dest string (which can be up to 255 chars)
- is long enough to hold the result.
- \*------------------------------------------------------------------------------*/
- OSErr FormatLong ( long num, short formatID, StringPtr dest)
- {
- xtnd80 x80 = {0,{0,0,0,0}} ;
- x80.man[0] = HiWord(num) ;
- x80.man[1] = LoWord(num) ;
- x80.exp = 0x4000 + 32 - 2 ; // 16414
- return FormatExtd80( (extended80*)&x80, formatID, dest ) ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- FormatFixed
- *------------------------------------------------------------------------------*
- This routine converts a Fixed number into a pascal string using
- a 'FMAT' resource and the ExtendedToString() call so that the resulting
- string will be formatted accrding to the user's setting in the Numbers
- control panel. It does this by converting the Fixed into an extended80
- and then calling FormatExtd80.
- It assumes that the dest string (which can be up to 255 chars)
- is long enough to hold the result.
- \*------------------------------------------------------------------------------*/
- OSErr FormatFixed ( Fixed num, short formatID, StringPtr dest)
- {
- xtnd80 x80 = {0,{0,0,0,0}} ;
- x80.man[0] = HiWord(num) ;
- x80.man[1] = LoWord(num) ;
- x80.exp = 0x4000 + 16 - 2 ; // 16398
- return FormatExtd80( (extended80*)&x80, formatID, dest ) ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- FormatSmallFract
- *------------------------------------------------------------------------------*
- This routine converts a SmallFract number into a pascal string using
- a 'FMAT' resource and the ExtendedToString() call so that the resulting
- string will be formatted accrding to the user's setting in the Numbers
- control panel. It does this by converting the SmallFract into an extended80
- and then calling FormatExtd80.
- It assumes that the dest string (which can be up to 255 chars)
- is long enough to hold the result.
- \*------------------------------------------------------------------------------*/
- OSErr FormatSmallFract ( short num, short formatID, StringPtr dest)
- {
- xtnd80 x80 = {0,{0,0,0,0}} ;
- x80.man[0] = num;
- x80.exp = 0x4000 + 1 - 2 ; // 16384
- return FormatExtd80( (extended80*)&x80, formatID, dest ) ;
- }
-
-
-